package com.sniper.web.utils;

import com.sniper.utils.ValidateUtil;
import org.msgpack.annotation.Message;

import java.io.Serializable;
import java.util.*;

/**
 * 通过封装把参数传递给试图
 * https://my.oschina.net/u/1431106/blog/202652
 *
 * @author sniper
 */
@Message
public class ParamsToHtml implements Serializable, Cloneable {

    private static final long serialVersionUID = -3812755897254713583L;
    //操作url
    private String sniperUrl;
    //元素个数
    private int htmlNum = 0;
    // 显示删除按钮
    private boolean delButton = true;
    //传递的删除属性值
    private String delButtonValue = "delete";
    //传递一个字符串属性，干啥都可以用
    private String key;
    //存放名称
    private Map<String, String> keys = new LinkedHashMap<>();
    //存放名称对应的数据
    private Map<String, Map<String, String>> params = new LinkedHashMap<>();
    //按钮列表
    private List<Button> buttons = new ArrayList<>();
    //下拉列表
    private List<Select> selects = new ArrayList<>();

    public int getHtmlNum() {
        return htmlNum;
    }


    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public Map<String, String> getKeys() {
        return keys;
    }

    public void setKeys(Map<String, String> keys) {
        this.keys = keys;
    }

    public String getKeyValue(String key) {
        if (keys.containsKey(key)) {
            return keys.get(key);
        }
        return key;
    }

    public Map<String, Map<String, String>> getParams() {
        return params;
    }

    public void setParams(Map<String, Map<String, String>> params) {
        htmlNum += params.size();
        this.params = params;
    }

    public Map<String, String> getMapValue(String key) {
        return params.get(key);
    }

    public String getMapValueString(String key, String nkey) {
        return params.get(key).get(nkey);
    }

    /**
     * 根据名字或者值查找select
     *
     * @param name
     * @return
     */
    public Select getSelect(String name) {
        Select select = new Select();
        if (selects.size() > 0 && ValidateUtil.isValid(name)) {
            Optional<Select> ps = selects.stream().filter(s -> name.equals(s.getType()) || name.equals(s.getValue())).findAny();
            if (ps.isPresent()) {
                select = ps.get();
            }
        }
        return select;
    }

    /**
     * 获取按钮
     *
     * @param name
     * @return
     */
    public Button getButton(String name) {
        Button button = createButton();
        if (buttons.size() > 0 && ValidateUtil.isValid(name)) {
            Optional<Button> ps = buttons.stream().filter(s -> name.equals(s.getName())).findAny();
            if (ps.isPresent()) {
                button = ps.get();
            }
        }
        return button;
    }

    /**
     * 为按钮添加赋值
     *
     * @param key
     * @param value
     */
    public void addMapValue(String key, Map<String, String> value) {
        htmlNum++;
        params.put(key, value);
    }

    public List<Button> getButtons() {

        buttons.sort(Comparator.comparingInt(Button::getOrder));

        return buttons;
    }

    /**
     * 添加按钮
     *
     * @param buttons
     */
    public void setButtons(List<Button> buttons) {
        htmlNum += buttons.size();
        this.buttons = buttons;
    }

    public List<Select> getSelects() {
        selects.sort(Comparator.comparingInt(Select::getOrder));
        return selects;
    }

    public void setSelects(List<Select> selects) {
        htmlNum += buttons.size();
        this.selects = selects;
    }

    public String getSniperUrl() {
        return sniperUrl;
    }

    public void setSniperUrl(String sniperUrl) {
        this.sniperUrl = sniperUrl;
    }

    public boolean isDelButton() {
        return delButton;
    }

    public void setDelButton(boolean delButton) {
        this.delButton = delButton;
    }

    public String getDelButtonValue() {
        return delButtonValue;
    }

    public void setDelButtonValue(String delButtonValue) {
        this.delButtonValue = delButtonValue;
    }


    @Override
    public String toString() {
        return "ParamsToHtml [keys=" + keys + ", params=" + params + "]";
    }

    public final Button createButton() {
        Button button = new Button();
        this.buttons.add(button);
        return button;
    }

    public final Select createSelect() {
        Select select = new Select();
        this.selects.add(select);
        return select;
    }

    /**
     * 颜色
     */
    @Message
    public static class Color implements Serializable, Cloneable {
        private static final long serialVersionUID = -5662312867738190383L;
        public static final String DEFAULT = "default";
        public static final String PRIMARY = "primary";
        public static final String SUCCESS = "success";
        public static final String INFO = "info";
        public static final String WARNING = "warning";
        public static final String DANGER = "danger";

    }


    /**
     * 按钮
     */

    @Message
    public static class Button implements Serializable, Cloneable {

        private static final long serialVersionUID = 1L;
        //现实的名称
        private String name;
        //穿的值 menuValue获取,一般按钮用不到
        private String value;
        //用于大新窗口的id
        private String target;
        // = menuType
        private String type;
        //支持点击事件
        private String click = "on";
        private String color = Color.DEFAULT;
        private String url;
        private int order;

        public Button() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getTarget() {
            return target;
        }

        public void setTarget(String target) {
            this.target = target;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getClick() {
            return click;
        }

        public void setClick(String click) {
            this.click = click;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public int getOrder() {
            return order;
        }

        public void setOrder(int order) {
            this.order = order;
        }
    }


    /**
     * 下拉
     */
    @Message
    public static class Select implements Serializable, Cloneable {

        private static final long serialVersionUID = -5505263857533770342L;
        private Map<String, String> values = new LinkedHashMap<>();
        //现实的名字
        private String type;
        //传递处理的类型
        private String value;
        private String color = Color.DEFAULT;
        private int order;

        public Select() {
        }

        public Map<String, String> getValues() {
            return values;
        }

        public void setValues(Map<String, String> values) {
            this.values = values;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public int getOrder() {
            return order;
        }

        public void setOrder(int order) {
            this.order = order;
        }
    }

}
